<template>
	<div class="zh-asset-import">
		<nav-header>
			<template slot="menuBefore">
                <li><router-link to="/affairs/asset/asset-list" tag="a">资产列表</router-link></li>
				<li class="gap">&gt;</li>
			</template>
		</nav-header>
        
		<div class="zh-import-comter">
            <el-button plain @click="downloadDemo">下载模板</el-button>
            <uploader 
                :options="uploadOptions" 
                :autoStart="false" 
                ref="uploader" 
                :file-status-text="statusText" 
                @files-submitted="fileSubmit"
                @file-removed="fileRemoved" 
                @file-success="fileSuccess" 
                @file-error="fileFail"  
                class="uploader-example">
                <uploader-unsupport></uploader-unsupport>
                <uploader-btn 
                    :directory="false" 
                    :attrs="attrs" 
                    v-if="selectFileShow" 
                    :single="true">
                    选择文件
                </uploader-btn>
                <uploader-list  v-if="!selectFileShow"></uploader-list>
            </uploader>
            <el-button type="primary" v-if="!selectFileShow" @click="fileUpload" style="margin-top: 10px;">确定导入</el-button>
		</div>
        
		<div class="zh-import-text">
			<p>注意事项: </p>
			<p>1、请从第二行开始导入数据，且不需要删除第一行的示例。</p>
			<p>2、表头为红色字体的为必填项，黑色字体的为选填项。</p>
			<p>3、购买时间格式请严格按照YYYY-MM-DD格式导入，如2019-08-07，否则系统无法正确识别。 </p>
			<p>4、备注输入字符限制在140字内，超出将导致导入失败。</p>
            <p>5、导入图片时请尽量将图片放置入对应 行/列 的格子中，导入时是按图片左上角坐标对应行导入。</p>
            <p>6、单个资产总数量自动根据资产对应的各个状态数量计算总和。</p>
            <p>7、导入图片格式仅支持jpg/png/jpeg/bmp。</p>
		</div>
		<div class="zh-import-table">
			<p>例：</p>
			<div class="zh-table">
				<div class="zh-table-contents">
                    <el-table
                        :data="importData"
                        border
                        fixed
                        style="width: 100%">
                        <el-table-column
                            prop="0"
                            label="序号"
                            align="center"
                            width="50px"
                            show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column
                            prop="1"
                            label="资产名称"
                            align="center"
                            width="150"
                            show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column
                            prop="2"
                            label="购买时间"
                            align="center"
                            width="120"
                            show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column
                            prop="3"
                            label="资产分类"
                            align="center"
                            width="130"
                            show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column
                            prop="4"
                            label="信息（品牌-字号）"
                            align="center"
                            width="200"
                            show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column
                            prop="5"
                            label="资产图片"
                            align="center"
                            width="120"
                            show-overflow-tooltip>
                            <template slot-scope="scope">
                                <img :src="scope.row.defaultImg" style="width: 80px" alt="">
                            </template>
                        </el-table-column>
                        <el-table-column
                            prop="6"
                            label="单位"
                            align="center"
                            width="120"
                            show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column
                            prop="7"
                            label="单位价格"
                            align="center"
                            width="120"
                            show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column
                            prop="8"
                            label="存放点"
                            align="center"
                            width="130"
                            show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column
                            prop="9"
                            label="归属"
                            align="center"
                            width="120"
                            show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column
                            prop="10"
                            label="保管人"
                            align="center"
                            width="130"
                            show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column
                            prop="11"
                            label="保管人联系方式"
                            align="center"
                            width="120"
                            show-overflow-tooltip>
                        </el-table-column>
                      <el-table-column label="资产状态" align="center" show-overflow-tooltip>>
                            <el-table-column
                                prop="12"
                                label="未使用"
                                align="center"
                                width="100"
					            show-overflow-tooltip>
                            </el-table-column>
                            <el-table-column
                                prop="13"
                                label="使用中"
                                align="center"
                                width="100"
					            show-overflow-tooltip>
                            </el-table-column>
                            <el-table-column
                                prop="14"
                                label="损坏"
                                align="center"
                                width="100"
					            show-overflow-tooltip>
                            </el-table-column>
                            <el-table-column
                                prop="15"
                                label="丢失"
                                align="center"
                                width="100"
					            show-overflow-tooltip>
                            </el-table-column>
                            <el-table-column
                                prop="16"
                                label="报废"
                                align="center"
                                width="100"
					            show-overflow-tooltip>
                            </el-table-column>
                        </el-table-column>
                        <el-table-column
                            prop="17"
                            label="备注"
                            align="center"
                            width="200"
                            show-overflow-tooltip>
                        </el-table-column>
                    </el-table>
				</div>
			</div>
		</div>
	</div>
</template>
<script>
    import defaultImg from './../../../../static/image/affairs/asset/defaultAsset1.png';
    import {downloadBatchDemo, uploaderFileFail, longTimeMessage} from './../../../tools/tool';

	export default {
	    data() {
		    return {
		    	messageObj:{},
		    	statusText: {
			      	success: '上传成功',  
			      	error: '上传出错',  
			     	uploading: '正在上传...',  
			      	paused: '暂停',  
			      	waiting: '等待上传'  
				},
				selectFileOrNot:false,  //是否选中文件
			    uploadOrNot:false, 		//是否已经上传
			    uploadInfo:{
					file_name:'',
					file_path:'',
					file_size:0
				},
				attrs: {
			      	accept: ['application/vnd.ms-excel','application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']
			    },
		    	uploadOptions: {
			          target: this.BASE_URL+'/api/user/vue_userdemo_upload',
			          testChunks: false,
			          headers:{Authorization : `Bearer `+this.$g.access_token},
			    },
		        fileList: [],
	            rules: {
	      		},
	      		selectFileShow:true,//是否显示选择文件按钮
				uploadRetryTimes:100,//文件最大重传次数
				importData: [{
                    0: '1',
                    1: 'LED（示例）',
                    2: '2019-08-05',
                    3: '电子设备',
                    4: '康佳-XR',
                    defaultImg: defaultImg,
                    6: '个',
                    7: '500',
                    8: '总务库房',
                    9: '三年2班',
                    10: '张三',
                    11: '15622189121',
                    12: '1',
                    13: '1',
                    14: '0',
                    15: '0',
                    16: '0',
                    17: '物品易碎，请妥善保管'
                }],
                excelUploader: null, // 上传组件引用
	      	}
        },
        mounted () {
            this.$nextTick(() => {  
                this.excelUploader = this.$refs.uploader.uploader;
            })  
		},
	    methods: {
            /**
             * 下载导出模板
             */
	    	downloadDemo(){
                let url = this.BASE_URL + '/api/property/multidemo';
                downloadBatchDemo(url);
	    	},
			submitForm(formName) {
				this.$refs[formName].validate((valid) => {
					if (valid) {
						if( this.selectFileOrNot ){
							this.fileUpload();
						}else{
							this.resourceSave();
						}
					} else {
						return false;
					}
				});
			},
	        resourceSave(){
				let resourceParam = {};
				if( this.uploadOrNot ) {
					resourceParam.path = this.uploadInfo.file_path;
				}
                resourceParam.class_id = this.$route.params.id;
                
            	let success = (data=>{
            		this.$message.success(data.msg);
                    
                    setTimeout(() => {
                        this.$router.go(-1);
                    }, 1000);
            	})
            	let fail = (res => {
                    longTimeMessage(this, res, 15000);
            	}) 
            	
				this.$ajax.post('api/property/multiexcel', resourceParam, {func:{success:success,fail:fail}})	
			},
			//上传成功的事件  
			fileSuccess (rootFile, file, message, chunk) { 
				this.selectFileShow = true;
                var mess = eval('(' + message + ')');
                mess = handleResponseData( mess );

			 	if(parseInt( mess.data.status ) ===1 ){
			  		this.uploadOrNot = true;
			  		this.uploadInfo.file_name = mess.data.file_name;
			  		this.uploadInfo.file_path = mess.data.file_path;
			  		this.uploadInfo.post_fix = mess.data.post_fix;
			  	
			  		this.resourceSave();
                }
                this.excelUploader.removeFile(file);
			},
			fileFail(rootFile, file, message, chunk){
                uploaderFileFail(this, this.excelUploader, file, message);
			},
			//过滤文件格式
			fileSubmit(files, fileList, event){
				for (var i = 0 ; i < fileList.length ; i++ ) {
                    if( this.excelUploader.fileList.length  > 1  ){
                        //删除第一张图片
                        this.excelUploader.removeFile( this.excelUploader.fileList[0] );
                    }
                    this.uploadInfo.file_size = fileList[i].size;
                    this.selectFileOrNot = true;
                    this.selectFileShow = false;
				}
			},
			fileRemoved(file){
				this.selectFileOrNot = false;
				this.selectFileShow = true;
			},
			fileUpload(){
				this.excelUploader.upload();
			},
	    },
		destroyed(to, from, next) {
            this.$message.closeAll();
		},
	}
</script>
<style lang="scss">
	.zh-asset-import {
		.zh-import-comter {
			margin-top: 20px;
		}
		.uploader-example {
			margin-top: 20px;
		}
		.zh-import-text {
			font-size: 12px;
		    color: #999;
		    line-height: 30px;
		    margin: 20px;
		    p {
		    	margin: 0;
		    }
		    span {
			    color: red;
			}
		}
		.zh-import-table {
			p {
				font-size: 16px;
				margin-bottom: 20px;
			}
		}
		.uploader-list {
		    .uploader-file-pause {
		      display: none !important;
		    }
		    .uploader-file-status {
		      text-indent: 0;
		    }
 		}

        .el-table__header-wrapper table thead {
            tr:first-child {
                th:nth-child(2),
                th:nth-child(13) {
                    color: red;
                }

                th:nth-child(13) {
                    border-bottom: inherit;
                }
            }

            tr:last-child {
                th:nth-child(1),
                th:nth-child(2),
                th:nth-child(3),
                th:nth-child(4),
                th:nth-child(5) {
                    color: red;
                }
            }
        }
	}
</style>